<template>
  <div>
    <div>
      <el-button type="primary" @click="router.push('/crud/newstrue')"
        >返回</el-button
      >
    </div>
    <div v-loading="loading">
      <el-card v-if="news.title">
        <template #header>
          <div class="news-title">
            <div>
              {{ news.title }}
            </div>
            <div>
              {{ news.source }}
              <br />
              {{ tools.formatDate(news.lastupdate) }}
            </div>
          </div>
        </template>

        <div class="news">
          <div v-html="news.info"></div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox } from 'element-plus'
import { api } from '../../js/ajax'
import { tools } from '../../js/tools'
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const router = useRouter()
const route = useRoute()

const loading = ref(false)

const nid = ref(route.query.nid)

const news = ref({})
const query = () => {
  loading.value = true
  api.get(`/crud/news/${nid.value}`, {}, data => {
    loading.value = false
    if (data.success) {
      news.value = data.data
      return
    }
    ElMessageBox.alert(data.message, '新闻管理').finally(() => {
      router.push('/crud/newstrue')
    })
  })
}

query()
</script>

<style scoped>
.news {
  width: 80%;
}

.news:deep p {
  text-indent: 2em;
}

.news:deep img {
  display: block;
  width: 100%;
  height: auto;
  margin: 1.5rem auto;
}

.news-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.news-title > div:first-of-type {
  font-size: 1.5rem;
}

.el-card {
  width: 80%;
  margin: 0 auto;
}
</style>
